package com.example.qy.controller;

import com.alibaba.fastjson.JSON;
import com.example.lxy.pojo.user;
import com.example.lxy.service.UserService;
import com.example.qy.pojo.sf;
import com.example.qy.pojo.wyf;
import com.example.qy.service.DfService;
import com.example.qy.service.SfService;
import com.example.qy.service.WyfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

import java.util.HashMap;
import java.util.List;

@Controller
@ResponseBody
public class SfController {
    @Autowired
    private SfService sfService;
    @CrossOrigin
    @PostMapping(value = "/api/listsf")
    public String listsf(@RequestBody user user) {
        String username = user.getUsername();
        username = HtmlUtils.htmlEscape(username);
        HashMap<String, Object> data = new HashMap<>();
        List<sf> list = sfService.select(username);
        if (list == null) {
            data.put("sfs",list);
        } else {
        }
        String s = JSON.toJSONString(data);
        System.out.println(list);
        System.out.println("hhh");
        return s;
    }
    @CrossOrigin
    @PostMapping(value = "/api/listsfall")
    public String listwyfall() {
        HashMap<String, Object> data = new HashMap<>();
        List<sf> list = sfService.selectall();
        if (list!=null) {
            data.put("sfs",list);
        } else {
        }
        String s = JSON.toJSONString(data);
        System.out.println(s);
        return s;
    }
    @CrossOrigin
    @PostMapping(value = "/api/addsf")
    public String add(@RequestBody sf sf) {
        System.out.println(sf);
        sfService.add(sf);
        return "true";
    }
    @CrossOrigin
    @PostMapping(value = "/api/deletesf")
    public String delete(@RequestBody int uid) {

        sfService.delete(uid);
        return "true";
    }
}
